/********************************************************************
Name: Figures 1, 2
Author: Dan Thompson
Date: September 2021
Description: 

********************************************************************/

clear all
set matsize 11000
set maxvar 15000
gl path = "~/Dropbox/Mariel Effects"

////
// Figure 1
////

use "$path/Replication/modified_data/synth_output555.dta", clear
twoway (line synt year if id==1, lc(gs2) lp(dash)) ///
	(line treat year if id==1, lc(gs2)), ///
	legend(order(2 "Miami-Dade" 1 "Synthetic Control")) ///
	yti("Rep Pres Vote Pct", si(medlarge)) ///
	xti("Year", si(medlarge)) ///
	xlab(, labs(medlarge)) ylab(, labs(medlarge)) ///
	xli(1980, lc(gs8) lp(shortdash)) ///
	graphregion(color(white)) ///
	name("original_synth_555", replace)
graph export "$path/Replication/output/original_synth_555.pdf", ///
	replace name("original_synth_555")


////
// Figure 2
////

use "$path/Replication/modified_data/synth_output555.dta", clear
replace diff = diff*100
gen diff2 = diff^2
egen tmp = mean(diff2) if year<1980, by(id)
egen mspe = max(tmp), by(id)
sum mspe if id==1
keep if mspe<5*`r(mean)'
drop tmp mspe diff2
reshape wide treat synth diff, i(year) j(id)
order year diff* treat* synth*
cap twoway (line diff3-diff37 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff41-diff68 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff73-diff100 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff101-diff125 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff126-diff150 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff151-diff174 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff176-diff199 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff201-diff222 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff228-diff249 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff251-diff274 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff280-diff300 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff302-diff322 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff328-diff349 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff352-diff375 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff377-diff400 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff401-diff424 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff426-diff450 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff455-diff471 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff477-diff500 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff504-diff516 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff526-diff550 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff1 year, lc(gs2) lw(vthin)), ///
	legend(order(187 "Miami-Dade" 1 "Synthetic Controls")) ///
	yti("Difference in Rep Pres Vote Pct", si(medlarge)) ///
	xt("Year", si(medlarge)) ///
	xli(1980, lc(gs8) lp(shortdash)) ///
	xlab(, labs(medlarge)) ylab(, labs(medlarge)) ///
	graphregion(color(white)) ///
	name("original_diff_synth_555", replace)
if _rc!=0 twoway (line diff3-diff37 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff41-diff68 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff73-diff100 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff101-diff125 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff126-diff150 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff151-diff174 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff176-diff199 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff201-diff222 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff228-diff249 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff251-diff274 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff281-diff300 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff302-diff322 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff328-diff349 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff352-diff375 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff377-diff400 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff401-diff424 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff426-diff450 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff455-diff471 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff477-diff500 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff504-diff516 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff526-diff550 year, lc(gs13 ...) lw(vthin ...)) ///
	(line diff1 year, lc(gs2) lw(vthin)), ///
	legend(order(187 "Miami-Dade" 1 "Synthetic Controls")) ///
	yti("Difference in Rep Pres Vote Pct", si(medlarge)) ///
	xt("Year", si(medlarge)) ///
	xli(1980, lc(gs8) lp(shortdash)) ///
	xlab(, labs(medlarge)) ylab(, labs(medlarge)) ///
	graphregion(color(white)) ///
	name("original_diff_synth_555", replace)
graph export "$path/Replication/output/original_diff_synth_555.pdf", ///
	replace name("original_diff_synth_555")


////
// Table A2
// Note: The table reported in the paper, was produced on a mac. When running synth 
// on a mac rather than a PC, three additional control units meet the criteria for
// inclusion in the placebo distribution used for Table A2. Based on where these three 
// control units are located in the placebo distribution, the location of the treated 
// unit in the placebo distribution changes very slightly. This only affects the 
// location of the treated unit in the placebo distribution in columns 1 and 2, but 
// it means that Miami's location in placebo distribution after running synth on your
// machine may not perfectly match the results reported in the paper. In all versions
// I have seen, Miami's location in the placebo distribution changes by no mor than 
// one tenth of one percentage point.
////

* Loop over the three control pools
qui foreach c in "250" "555" "_florida" {

	* Bring in the synthetic control output
	use "$path/Replication/modified_data/synth_output`c'.dta", clear

	* Remove the cases that have especially poor synthetic controls pre-treatment
	gen diff2 = diff^2
	sort id year
	egen mspe = mean(diff2) if year<1980, by(id)
	by id: carryforward mspe, replace
	drop diff2
	sum mspe if id==1
	keep if mspe<5*`r(mean)'
	tempfile in_sample
	save `in_sample', replace
	
	* Compute effect over whole post-treatment period
	use if year>=1980 using `in_sample', clear
	collapse diff, by(id)
	sort diff
	gen percentile = _n/(_N+1)
	sum diff if id==1
	local imp1976_`c' = `r(mean)'
	sum percentile if id==1
	local ptile1976_`c' = 100*`r(mean)'
	
	* Compute effect in each post-treatment year
	forval y=1980(4)2000 {
		use if year==`y' using `in_sample', clear
		sort diff
		gen percentile = _n/(_N+1)
		sum diff if id==1
		local imp`y'_`c' = `r(mean)'
		sum percentile if id==1
		local ptile`y'_`c' = 100*`r(mean)'
	}
}

quietly {
	cap log close
	log using "$path/Replication/output/synth_by_yr_table.tex", text replace
	noisily dis "\begin{table}[ht]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{Increase in Republican Presidential Vote Share "
	noisily dis "After the Mariel Boatlift, Synthetic Control.}\label{tab:synth_yr}}"
	noisily dis "\begin{tabular}{cccc}"
	noisily dis "\toprule \toprule"
	noisily dis " & \multicolumn{3}{c}{Counties in Control Pool} \\"
	noisily dis " & 250 & 555 & Florida \\[1mm]"
	noisily dis "\midrule"
	noisily dis " All Years & " %3.2f `imp1976_250' " & " %3.2f `imp1976_555' " & " %3.2f `imp1976__florida' " \\"
	noisily dis " & [" %3.2f `ptile1976_250' "] & [" %3.2f `ptile1976_555' "] & [" %3.2f `ptile1976__florida' "] \\[2mm]"
	noisily dis " 1980 & " %3.2f `imp1980_250' " & " %3.2f `imp1980_555' " & " %3.2f `imp1980__florida' " \\"
	noisily dis " & [" %3.2f `ptile1980_250' "] & [" %3.2f `ptile1980_555' "] & [" %3.2f `ptile1980__florida' "] \\[2mm]"
	noisily dis " 1984 & " %3.2f `imp1984_250' " & " %3.2f `imp1984_555' " & " %3.2f `imp1984__florida' " \\"
	noisily dis " & [" %3.2f `ptile1984_250' "] & [" %3.2f `ptile1984_555' "] & [" %3.2f `ptile1984__florida' "] \\[2mm]"
	noisily dis " 1988 & " %3.2f `imp1988_250' " & " %3.2f `imp1988_555' " & " %3.2f `imp1988__florida' " \\"
	noisily dis " & [" %3.2f `ptile1988_250' "] & [" %3.2f `ptile1988_555' "] & [" %3.2f `ptile1988__florida' "] \\[2mm]"
	noisily dis " 1992 & " %3.2f `imp1992_250' " & " %3.2f `imp1992_555' " & " %3.2f `imp1992__florida' " \\"
	noisily dis " & [" %3.2f `ptile1992_250' "] & [" %3.2f `ptile1992_555' "] & [" %3.2f `ptile1992__florida' "] \\[2mm]"
	noisily dis " 1996 & " %3.2f `imp1996_250' " & " %3.2f `imp1996_555' " & " %3.2f `imp1996__florida' " \\"
	noisily dis " & [" %3.2f `ptile1996_250' "] & [" %3.2f `ptile1996_555' "] & [" %3.2f `ptile1996__florida' "] \\[2mm]"
	noisily dis " 2000 & " %3.2f `imp2000_250' " & " %3.2f `imp2000_555' " & " %3.2f `imp2000__florida' " \\"
	noisily dis " & [" %3.2f `ptile2000_250' "] & [" %3.2f `ptile2000_555' "] & [" %3.2f `ptile2000__florida' "] \\[2mm]"
	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{4}{p{.4\textwidth}}{\footnotesize"
	noisily dis "All estimates calculated holding out counties that border Miami-Dade. "
	noisily dis "The 250 or 555 counties most similar to Miami-Dade in terms of 1978 population "
	noisily dis "are included in the control pool. "
	noisily dis "The percentile of the placebo distribution where Miami-Dade lands is reported in "
	noi di "square brackets. "
	noisily dis "The placebo distribution is found using the procedure discribed in "
	noisily dis "Abadie et al (2010).}"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	log off
}

